package org.itboys.core.service.mysql.web;

import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.store.SundayStoreAddMapper;
import org.itboys.core.dao.store.SundayStoreNumMapper;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.entity.mysql.SundayStoreAdd;
import org.itboys.core.entity.mysql.SundayStoreNum;
import org.itboys.core.service.mongo.web.SundayStoreService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/22
 * @Description：库存增量表
 */
@Service
public class SundayStoreAddService extends BaseMySqlService<SundayStoreAdd>{
    @Autowired
    private SundayStoreAddMapper sundayStoreAddMapper;
    @Autowired
    private SundayStoreNumMapper storeNumMapper;
    @Autowired
    private SundayStoreService storeService;

    public void insert(List<SundayStoreAdd> storeAddList){
        for (SundayStoreAdd storeAdd:storeAddList
             ) {
            if(AdminSessionHolder.getAdminUserId()!=null&&AdminSessionHolder.getAdminUserId().longValue()!=0l){
                storeAdd.setCreator(AdminSessionHolder.getAdminUserId());
            }else{
                storeAdd.setCreator(0l);
            }
            if(AdminSessionHolder.getAdminUserId()!=null&&AdminSessionHolder.getAdminUserId().longValue()!=0l){
                storeAdd.setUpdater(AdminSessionHolder.getAdminUserId());
            }else{
                storeAdd.setUpdater(0l);
            }
        }
        if(ListUtil.isNotNull(storeAddList)){
            sundayStoreAddMapper.insertStoreAdds(storeAddList);
        }
    }

    /**
     * 批量生成仓库增量,同时更新库存
     * @param storeAddList
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(List<SundayStoreAdd> storeAddList,Long preStoreId){
        insert(storeAddList);
        List<Long> skuIds = storeNumMapper.selectSKUIdsByStoreId(preStoreId);
        List<Long> updateIds=new ArrayList<>();
        List<Long> insertIds=new ArrayList<>();
        for (SundayStoreAdd storeAdd:storeAddList){
            updateIds.add(storeAdd.getSKUId());
            insertIds.add(storeAdd.getSKUId());
        }
        //求出交集更新，增量表减去更新的得到需要新增的库存数据
        updateIds.retainAll(skuIds);
        insertIds.removeAll(updateIds);
        List<SundayStoreAdd> updateList=new ArrayList<>();
        List<SundayStoreAdd> insertList=new ArrayList<>();
        for (SundayStoreAdd storeAdd:storeAddList) {
            for (Long updateId:updateIds) {
                if(storeAdd.getSKUId().equals(updateId)){
                    updateList.add(storeAdd);
                    break;
                }
            }
            for (Long insertId:insertIds) {
                if(storeAdd.getSKUId().equals(insertId)){
                    insertList.add(storeAdd);
                    break;
                }
            }
        }
        if(ListUtil.isNotNull(insertList)){
            storeNumMapper.insertOrderNums(insertList);
        }
        if(ListUtil.isNotNull(updateList)&& LongUtil.isNotZreo(preStoreId)){
            storeNumMapper.updateByStoreId(preStoreId,updateList);
        }
    }
    /**
     * 获取增量数据
     * @param request
     * @return
     */
    public PageResult<SundayStoreAdd> select(HttpServletRequest request){
        PageResult result=new PageResult<SundayStoreAdd>();
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);
        List<SundayStoreAdd> data = super.select(param);
        if (!ListUtil.isNotNull(data)){
            return result;
        }
        List<Long> storeIds=new ArrayList<>();
        for (SundayStoreAdd storeAdd :data
                ) {
            storeIds.add(storeAdd.getStoreId());
        }
        //组装仓库信息
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("id in",storeIds);
        List<SundayStore> stores = storeService.list(param);
        for (SundayStoreAdd storeAdd :data
                ) {
            for (SundayStore store:stores
                 ) {
                if(storeAdd.getStoreId().equals(store.getId())){
                    storeAdd.setStoreName(store.getName());
                }
            }
        }
        result.setData(data);
        result.setTotal(count(param));
        return result ;
    }

}
